//
//  CZStatus.swift
//  CZWeibo
//
//  Created by 周振兰 on 16/5/17.
//  Copyright © 2016年 周振兰. All rights reserved.
//

import UIKit
// ///微博模型
class CZStatus: NSObject
{
 ///微博时间
    var created_at:String?
    
 ///微博ID
    var id:Int64 = 0
    
  ///微博信息的内容
    var text:String?
    
    ///微博来源
    var source:String?
    
    ///转发数
    var reposts_count:Int  = 0
    
    /// 评论数
    var comments_count: Int = 0
    
    /// 表态数
    var attitudes_count: Int = 0
    
    /// 微博配图: 数组里面是字典 // Int类型的数组[Int], 字典类型的数组 [String: AnyObject] -> [[String: AnyObject]]
    /*[{"thumbnail_pic":"http://ww2.sinaimg.cn/thumbnail/e9492957gw1f3wde5ykd3j20go0gomyp.jpg"},{"thumbnail_pic":"http://ww2.sinaimg.cn/thumbnail/e9492957gw1f3wde7z6u8j20go0gigo2.jpg"},{"thumbnail_pic":"http://ww1.sinaimg.cn/thumbnail/e9492957gw1f3wdeahtw6j20go0h3juf.jpg"},{"thumbnail_pic":"http://ww3.sinaimg.cn/thumbnail/e9492957gw1f3wdebyovwj20go0go0tv.jpg"},{"thumbnail_pic":"http://ww3.sinaimg.cn/thumbnail/e9492957gw1f3wdednivnj20go0goabn.jpg"},{"thumbnail_pic":"http://ww2.sinaimg.cn/thumbnail/e9492957gw1f3wdefgkrqj20go0gogni.jpg"},{"thumbnail_pic":"http://ww1.sinaimg.cn/thumbnail/e9492957gw1f3wdego3awj20go0go755.jpg"},{"thumbnail_pic":"http://ww3.sinaimg.cn/thumbnail/e9492957gw1f3wdeix2f8j20go0goq53.jpg"},{"thumbnail_pic":"http://ww1.sinaimg.cn/thumbnail/e9492957gw1f3wdekf8xpj20go0gowf7.jpg"}]
    */
    var pic_urls: [[String: AnyObject]]?{
        
        didSet{
            guard let urlDicts = pic_urls else {
                print("配图没有值")
                return
            }
            
            //初始化空数组
            pictureURLs = [NSURL]()
            for dict in urlDicts {
                
                
                if let urlString = dict["thumbnail_pic"] as? String{
                    
                    let url = NSURL(string: urlString)
                    //将转好的url放在pictureURLs数组中
                    pictureURLs?.append(url!)
                }
            }
        }
    }
    //模型提供一个完善的图片地址数组，后续使用方便
    var pictureURLs:[NSURL]?
    //用户模型
    var user:CZUser?
    //被转发微博模型
    var retweeted_status:CZStatus?
    
    ///字典转模型
    init(dic:[String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dic)
        //setValue:forKey,字典有几个key就调用几次
    }
    
    //相当于嵌套模型转字典
    override func setValue(value: AnyObject?, forKey key: String) {
        CZPrint(items: "拦截到了微博模型在赋值 user 这个key: \(value)")
        //value是一个字典
        if key == "user"
        {
            if let dict = value as? [String:AnyObject]
            {
                //将自己的user这个属性赋值CZUser
                user = CZUser(dict: dict)
            }
            //一定要记得return,不然会不给super.setValue(value, forKey: key)：给覆盖成字典
            return
        }
        
        if key == "retweeted_status"{
            //拦截，自己来转模型
            if let dict = value as? [String:AnyObject]{
                //字典转模型
                retweeted_status = CZStatus(dic: dict)
            }
            // 一定要记得return,不然会不给super.setValue:forKey:给覆盖成字典
            return
        }
        super.setValue(value, forKey: key)
    }
    ///字典中的key在模型中找不到相对应的值，实现这个方法就不会报错
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    //对象打印
  override  var description: String
        {
        get
        {
          
           let keys = ["created_at", "id", "text", "source", "reposts_count", "comments_count", "attitudes_count", "pic_urls"]
            return "\n \t 微薄数据：\( dictionaryWithValuesForKeys(keys))"
        }
    }
    
    
}
